﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data.Items;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using Workflow.Alerts.DAL;
using System.Data;
using System.Text;
using System.Globalization;


namespace Workflow.Alerts
{
    public class MapPresenter : ListPresenter
    {
        #region members
        private readonly IMapOneView view;
        #endregion

        #region constructor
        public MapPresenter(IMapOneView _view)
            : this(_view, null)
        { }

        public MapPresenter(IMapOneView _view, ISessionProvider session)
            : base(_view, session)
        {
            view = base.GetView<IMapOneView>();
            view.OnMapLoad += view_OnLoad;
            model = new MainListDAL();
        }

        #endregion

        #region Events handlers

        protected override void view_OnLoad()
        {
            base.view_OnLoad();

            var alerts = GetList();

            if (alerts.Any())
            {
                Markers markers = FillMarkers(alerts);
                view.Markers = SerializeToString(markers);
            }
        }

        #endregion

        #region Private Methods

        private string SerializeToString(Markers markers)
        {
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer xms = new XmlSerializer(typeof(Markers));
                var xmlnsEmpty = new XmlSerializerNamespaces();
                xmlnsEmpty.Add("", "");
                xms.Serialize(sw, markers, xmlnsEmpty);
                return sw.ToString();
            }
        }

        private void GetCoordinatesFromString(string coordinates, ref string lat, ref string lng)
        {
            var coordinatesArr = coordinates.Split(new char[] { ';' });
            lat = coordinatesArr[0];
            lng = coordinatesArr[1];
        }

        protected override void SetWorkflowFilter(string item)
        {
            base.SetWorkflowFilter(item);

            //Sitecore.Data.Fields.CheckboxField mapField = (Sitecore.Data.Fields.CheckboxField)Sitecore.Context.Item.Parent.Fields["Map"];
            //if (mapField != null && !mapField.Checked)
            //{
                workflowFilter.AlertId = string.IsNullOrEmpty(view.Alertid) ? null : (Nullable<int>)Int32.Parse(view.Alertid);
            //}
        }

        private Markers FillMarkers(DataRow[] alerts)
        {
            Markers markers = new Markers();
            foreach (EstateDataSet.ListAlertRow alert in alerts)
            {
                var m = new marker()
                {
                    itemId = alert.id.ToString(),
                    type = ((WorkflowEnums.Status)alert.currentstatus).ToString()
                };
                GetCoordinatesFromString(alert.coordinates, ref m.lat, ref m.lng);
                markers.Add(m);
            }
            return markers;
        }

        #endregion
    }

    #region Define XML file
    [XmlRoot(ElementName = "markers")]
    public class Markers : System.Collections.Generic.List<marker>
    {
        public Markers() { }
    }

    public class marker
    {
        [XmlAttribute]
        public string lat;
        [XmlAttribute]
        public string lng;
        [XmlAttribute]
        public string type;
        [XmlAttribute]
        public string itemId;

        public marker() { }

    }
    #endregion
}
